package com.caiwangdai.app.origin

import android.graphics.drawable.Drawable
import android.os.Build
import android.support.annotation.ColorRes
import android.support.annotation.DrawableRes
import android.support.v4.content.ContextCompat
import android.support.v7.app.AppCompatActivity
import android.support.v7.widget.Toolbar
import android.text.TextUtils
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import com.caiwangdai.app.R
import com.caiwangdai.app.extension.getStatusBarHeight
import com.caiwangdai.app.extension.gone
import com.caiwangdai.app.extension.setOnClickListenerPro
import com.caiwangdai.app.utils.StatusBarUtil
import com.caiwangdai.app.utils.SystemUtil
import com.flyco.tablayout.SegmentTabLayout
import kotlinx.android.synthetic.main.toolbar_primary.view.*


class ToolBarManager(private val mActivity: AppCompatActivity, private val mContent: ViewGroup) {

    private var mParent: ViewGroup? = null
    private var mStatusCover: View? = null//状态栏遮罩
    private var mToolBar: Toolbar? = null
    /**
     * 获取标题视图

     * @备注 若有特殊处理，请获取此视图进行修改
     */
    var title: TextView? = null
        private set
    var tab: SegmentTabLayout? = null
        private set

    private var mMenuTextViewLeft: TextView? = null
    private var mMenuTextView: TextView? = null
    private var mMenuIconRight: ImageView? = null
    private var mMenuIconLeft: ImageView? = null

    private var BackGroundColor = -1


    init {
        initToolbar(this.mContent)
    }


    private fun initToolbar(mContent: ViewGroup?) {
        if (mContent == null) {
            return
        }
        //toolbar
        mToolBar = mContent.toolbar
        if (mToolBar == null)
            return


        //mParent
        mParent = mContent.toolbar_parent

        //status cover
        mStatusCover = mContent.view_cover
        //auto set height
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
            mStatusCover!!.minimumHeight = 0
            mStatusCover!!.visibility = View.GONE
        } else {
            val statusBarHigh = mActivity.getStatusBarHeight()
            mStatusCover!!.minimumHeight = statusBarHigh
        }

        //title
        title = mContent.toolbar_title
        tab = mContent.toolbar_tab
        mMenuIconRight = mContent.toolbar_menu_icon_right
        mMenuIconLeft = mContent.toolbar_menu_icon_left


        //textmenu
        mMenuTextView = mContent.toolbar_menu_text
        mMenuTextViewLeft = mContent.toolbar_menu_text_left


        //setup
        mActivity.setSupportActionBar(mToolBar)
        val actionBar = mActivity.supportActionBar
        actionBar!!.title = ""
        actionBar.setDisplayHomeAsUpEnabled(false)
        setNavigationIcon(-1)
        mToolBar!!.setNavigationOnClickListener { mActivity.onBackPressed() }

        mToolBar!!.navigationIcon = null

    }


    /**
     * 设置Toolbar的背景颜色--资源文件
     */
    fun setBackgroundColor(@ColorRes color: Int): ToolBarManager {
        val BgColor = ContextCompat.getColor(mActivity, color)
        //兼容小米魅族
        if (color == R.color.white) {
            if (TextUtils.equals(SystemUtil.getSystem(),SystemUtil.SYS_MIUI)) {
                StatusBarUtil.setMIUIStatusBarLightMode(mActivity,true)
            }
            if(TextUtils.equals(SystemUtil.getSystem(),SystemUtil.SYS_FLYME)){
                StatusBarUtil.setFlymeStatusBarLightMode(mActivity,true)
            }
        }
        mParent?.setBackgroundColor(BgColor)
        BackGroundColor = BgColor
        return this
    }


    /**
     * 设置Tollbar的背景-drawable
     */
    fun setBackground(background: Drawable): ToolBarManager {
        mParent!!.background = background
        return this
    }

    /**
     * 设置Tollbar的背景-DrawableRes
     */
    fun setBackground(@DrawableRes drawableRes: Int): ToolBarManager {

        mParent!!.background = ContextCompat.getDrawable(mActivity, drawableRes)
        return this
    }


    /**
     * 设置左边的按钮Icon
     * 备注：若不想显示图片，可以输入-1或者0
     */
    fun setNavigationIcon(@DrawableRes resId: Int): ToolBarManager {
        if (resId != 0 && resId != -1) {
            mActivity.supportActionBar!!.setDisplayHomeAsUpEnabled(true)
            mToolBar!!.setNavigationIcon(resId)
        } else {
            mToolBar!!.navigationIcon = null
        }
        return this
    }

    /**
     * 设置标题内容
     */
    fun setTitle(charsequence: CharSequence): ToolBarManager {
        title?.text = charsequence
        return this
    }

    fun setTabContent(titles: Array<String>): ToolBarManager {
        title?.visibility = View.GONE
        tab?.visibility = View.VISIBLE
        tab?.setTabData(titles)
        return this
    }


    /**
     * 设置标题颜色
     */
    fun setTitleColor(@ColorRes color: Int): ToolBarManager {
        val TextColor = ContextCompat.getColor(mActivity, color)
        title?.setTextColor(TextColor)
        return this
    }


    /**
     * 设置【右侧文本菜单】的内容
     */
    fun setMenuTextContent(charsequence: CharSequence): ToolBarManager {
        mMenuTextView?.visibility = View.VISIBLE
        mMenuTextView?.text = charsequence
        return this
    }


    /**
     * 设置【右侧文本菜单】标题颜色
     */
    fun setMenuTextColor(@ColorRes color: Int): ToolBarManager {
        mMenuTextView?.visibility = View.VISIBLE
        val TextColor = ContextCompat.getColor(mActivity, color)
        mMenuTextView?.setTextColor(TextColor)
        return this
    }

    /**
     * 设置【右侧文本菜单】点击事件
     */
    fun setMenuTextClick(onMenuTextClickListener: OnMenuTextClickListener?): ToolBarManager {
        mMenuTextView?.setOnClickListenerPro {
            onMenuTextClickListener?.onMenuTextClick()
        }
        return this
    }


    /**
     * 设置【左侧文本菜单】的内容
     */
    fun setLeftMenuTextContent(charsequence: CharSequence): ToolBarManager {
        mMenuTextViewLeft?.visibility = View.VISIBLE
        mMenuTextViewLeft?.setOnClickListener(null)
        mMenuTextViewLeft?.text = charsequence
        return this
    }


    /**
     * 设置【左侧文本菜单】标题颜色
     */
    fun setLeftMenuTextColor(@ColorRes color: Int): ToolBarManager {
        mMenuTextViewLeft?.visibility = View.VISIBLE
        val TextColor = ContextCompat.getColor(mActivity, color)
        mMenuTextViewLeft?.setTextColor(TextColor)
        return this
    }

    /**
     * 设置【左侧文本菜单】点击事件
     */
    fun setLeftMenuTextClick(onMenuTextClickListener: OnMenuTextClickListener?): ToolBarManager {
        mMenuTextViewLeft?.setOnClickListenerPro {
            onMenuTextClickListener?.onMenuTextClick()
        }
        return this
    }


    /**
     * @param resId
     * *
     * @return ToolBarManager
     * *
     * @description 设置右侧icon
     * *
     * @remark
     */
    fun setRightMenuIconRes(@DrawableRes resId: Int): ToolBarManager {
        mMenuIconRight?.visibility = View.VISIBLE
        mMenuIconRight?.setImageResource(resId)
        return this
    }

    /**
     * @param listener
     * *
     * @return ToolBarManager
     * *
     * @description 设置右侧icon点击事件
     * *
     * @remark
     */
    fun setRightMenuIconClickListener(listener: View.OnClickListener): ToolBarManager {
        mMenuIconRight!!.setOnClickListenerPro(listener)
        return this
    }

    /**
     * @param resId
     * *
     * @return ToolBarManager
     * *
     * @description 设置左侧icon
     * *
     * @remark
     */
    fun setLeftMenuIconRes(@DrawableRes resId: Int): ToolBarManager {
        mMenuIconLeft!!.visibility = View.VISIBLE
        mMenuIconLeft!!.setImageResource(resId)
        return this
    }

    val leftMenuIcon: View
        get() = mMenuIconLeft!!

    /**
     * @param listener
     * *
     * @return ToolBarManager
     * *
     * @description 设置左侧icon点击事件
     * *
     * @remark
     */
    fun setLeftMenuIconClickListener(listener: View.OnClickListener): ToolBarManager {
        mMenuIconLeft!!.setOnClickListenerPro(listener)
        return this
    }


    /**
     * 【右侧文本菜单】的接口
     */
    interface OnMenuTextClickListener {
        fun onMenuTextClick()
    }


    /**
     * 设置兼容系统窗口

     * @param activity
     */
    fun fitSystemWindow() {
        mStatusCover!!.minimumHeight = 0
        mStatusCover!!.visibility = View.GONE
    }

    /**
     * toolbar本身的点击事件
     */
    fun setOnclickListener(l: View.OnClickListener?) {
        mToolBar!!.setOnClickListenerPro(l!!)
    }

    fun hideToolBar() {
        mToolBar?.gone()
    }

    companion object {

        fun with(mActivity: AppCompatActivity, mContent: ViewGroup): ToolBarManager {
            return ToolBarManager(mActivity, mContent)
        }
    }


}
